package com.dx.mes.service.brm.service;

import com.dx.common.table.ppt.Bis_pam;
import com.dx.common.table.ppt.Bis_pamId;
import com.dx.mes.common.dao.IBisPamDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.brm.service.IFbpbispamService;
import com.dx.mes.facade.brm.tx.Fbpbispam.FbpbispamI;
import com.dx.mes.facade.brm.tx.Fbpbispam.FbpbispamIA;
import com.dx.mes.facade.brm.tx.Fbpbispam.FbpbispamO;
import com.dx.mes.facade.brm.tx.Fbpbispam.FbpbispamOA;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fbpbispamService")
@SuppressWarnings({"unchecked"})
public class FbpbispamService implements IFbpbispamService {
	private static final String TX_NAME = T_FBPBISPAM;
	private LogUtils logUtils;

	@Autowired
	private IBisPamDao bisPamDao;
	//@Autowired
	/*private IBisMaskDao bisMaskDao;*/

	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(FbpbispamService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FbpbispamService.class);
		logUtils.info("[InTrx:" + strInTrx + "]");

		Session session = bisPamDao.getCrSession();
		FbpbispamO outTrx = new FbpbispamO();
		String strOutTrx = null;

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		//输入对象
		try {
			FbpbispamI inTrx = new FbpbispamI();
			//将json字符串转换成对象
			inTrx = JacksonUtil.fromJson(strInTrx, FbpbispamI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);

			switch (action_flg) {
				case ACTION_FLG_QUERY:
					queryByCondition(inTrx,outTrx);
					break;
				case ACTION_FLG_ADD:
					addFnc(inTrx,outTrx);
					break;
				case ACTION_FLG_UPDATE:
					updateFnc(inTrx,outTrx);
					break;
				case ACTION_FLG_DELETE:
					deleteFnc(inTrx,outTrx);
					break;
				case ACTION_FLG_QUERYITEM:
					queryItemFnc(inTrx,outTrx);
					break;
				case 'B':
					isContianRecipe(inTrx,outTrx);
					break;
				case 'R':
					queryRecipeByTol(inTrx,outTrx);
					break;
				default:
					errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//			logUtils.pringStackTrace(ex);
		} finally {
			//将一个 Object 或者List对象转化为JSOObject或者JSONArray
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}

	private void queryRecipeByTol(FbpbispamI inTrx, FbpbispamO outTrx) {
		List<FbpbispamOA> oaryList = new ArrayList<FbpbispamOA>();
		FbpbispamIA iary=inTrx.getIary().get(0);
		String toolId=iary.getTool_id_fk();
		String hql;
		if (StringUtils.isNotBlank(toolId)) {
			hql = "SELECT distinct p.recipe_id FROM BIS_PAM p where p.tool_id_fk='" + toolId + "'";
		}else{
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg("Tool的id不能为空");
			return;
		}
		List<String> bispamList = bisPamDao.findBySQL(hql);
		for (String recipe_id : bispamList) {
			FbpbispamOA oary = new FbpbispamOA();
			oary.setRecipe_id(recipe_id);
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(RETURN_CODE_OK);
	}

	private void isContianRecipe(FbpbispamI inTrx, FbpbispamO outTrx) {
		FbpbispamIA iary=inTrx.getIary().get(0);
		String toolId=iary.getTool_id_fk();
		String recipeId = iary.getRecipe_id();
		String hql = "FROM Bis_pam where 1=1 ";
		if (StringUtils.isNotBlank(toolId)) {
			if (toolId.indexOf("*") == -1) {// 如果不包含*,精确查询
				hql = hql + "and tool_id_fk='" + toolId + "'";
			} else {// 模糊查询
				hql = hql + "and tool_id_fk like '" + toolId.replace("*", "%") + "'";
			}
		}
		if (StringUtils.isNotBlank(recipeId) ) {
			if (recipeId.indexOf("*") == -1) {// 如果不包含*,精确查询
				hql = hql + "and recipe_id='" + recipeId + "'";
			} else {// 模糊查询
				hql = hql + "and recipe_id like '" + recipeId.replace("*", "%") + "'";
			}
		}
		List<Bis_pam> bispamList = bisPamDao.find(hql);
		if (bispamList.size() > 0) {
			outTrx.setRtn_code(RETURN_CODE_OK);
		}else {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg("bispamList is zero in length");
		}
	}

	//查询对话框根据7个主键查询
	public void queryByCondition(FbpbispamI inTrx,FbpbispamO outTrx){
		try{
			FbpbispamIA iary=inTrx.getIary().get(0);
			String mdlId=iary.getMdl_id_fk();
			String toolId=iary.getTool_id_fk();
			//站点序号
			String opeNo=iary.getOpe_no_fk();
			String opeId=iary.getOpe_id_fk();
			String pathId =iary.getPath_id_fk();
			String pathVer = iary.getPath_ver_fk();
			String opeVer =iary.getOpe_ver_fk();

			List<FbpbispamOA> oaryList = new ArrayList<FbpbispamOA>();
			String hql = "select MAX(tool_id_fk) AS tool_id_fk,mdl_id_fk,path_id_fk,path_ver_fk," +
					"ope_no_fk,ope_id_fk,ope_ver_fk " +
						/*"recipe_id,mask_set_id_fk,active_flg," +
						"skip_flg,mes_id,box_set_code,swh_cnt_add_flg,swh_cnt_add_pos,swh_max_cnt " +*/
					"from bis_pam  where 1=1 ";

			if (StringUtils.isNotBlank(mdlId) ) {
				if (mdlId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + " and mdl_id_fk='" + mdlId + "'";
				} else {// 模糊查询
					hql = hql + " and mdl_id_fk like '" + mdlId.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(toolId)) {
				if (toolId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + " and tool_id_fk='" + toolId + "'";
				} else {// 模糊查询
					hql = hql + " and tool_id_fk like '" + toolId.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(opeId) ) {
				if (opeId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + " and ope_id_fk='" + opeId + "'";
				} else {// 模糊查询
					hql = hql + " and ope_id_fk like '" + opeId.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(pathId)) {
				if (pathId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + " and path_id_fk='" + pathId + "'";
				} else {// 模糊查询
					hql = hql + " and path_id_fk like '" + pathId.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(pathVer)) {
				if (pathVer.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + " and path_ver_fk='" + pathVer + "'";
				} else {// 模糊查询
					hql = hql + " and path_ver_fk like '" + pathVer.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(opeVer) ) {
				if (opeVer.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + " and ope_ver_fk='" + opeVer + "'";
				} else {// 模糊查询
					hql = hql + " and ope_ver_fk like '" + opeVer.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(opeNo)) {
				if (opeNo.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + " and ope_no_fk='" + opeNo + "'";
				} else {// 模糊查询
					hql = hql + " and ope_no_fk like '" + opeNo.replace("*", "%") + "'";
				}
			}
			hql = hql +	" group by mdl_id_fk,path_id_fk,path_ver_fk,ope_no_fk,ope_id_fk,ope_ver_fk";
//			Session session = bisPamDao.getCrSession();
//
//			Query query = session.createSQLQuery(hql)
//					.addScalar("tool_id_fk", Hibernate.STRING)
//					.addScalar("mdl_id_fk", Hibernate.STRING)
//					.addScalar("path_id_fk", Hibernate.STRING)
//					.addScalar("path_ver_fk", Hibernate.STRING)
//					.addScalar("ope_no_fk", Hibernate.STRING)
//					.addScalar("ope_id_fk", Hibernate.STRING)
//					.addScalar("ope_ver_fk", Hibernate.STRING)
//					.setResultTransformer(Transformers.aliasToBean(BisPamPo.class));
			List<Object[]> bispamList= bisPamDao.findBySQL(hql);

			for(int i = 0; i < bispamList.size(); i++){
				FbpbispamOA oary = new FbpbispamOA();
				Object[] tmpObj = bispamList.get(i);

				oary.setTool_id_fk(tmpObj[0].toString());
				oary.setMdl_id_fk(tmpObj[1].toString());
				oary.setPath_id_fk(tmpObj[2].toString());
				oary.setPath_ver_fk(tmpObj[3].toString());
				oary.setOpe_no_fk(tmpObj[4].toString());
				oary.setOpe_id_fk(tmpObj[5].toString());
				oary.setOpe_ver_fk(tmpObj[6].toString());
				oaryList.add(oary);
			}
			outTrx.setOary(oaryList);
			outTrx.setTbl_cnt(oaryList.size());
		}catch (Exception ex) {
			logUtils.error("[" + Util.stackTraceToString(ex) + "]");
			outTrx.setRtn_code(E_XPLSTDAT_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.toString());
			return;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}


	public void queryItemFnc(FbpbispamI inTrx,FbpbispamO outTrx){
		try{
			FbpbispamIA iary=inTrx.getIary().get(0);
			String mdlId=iary.getMdl_id_fk();
			String toolId=iary.getTool_id_fk();
			//站点序号
			String opeNo=iary.getOpe_no_fk();
			String opeId=iary.getOpe_id_fk();
			String pathId =iary.getPath_id_fk();
			String pathVer = iary.getPath_ver_fk();
			String opeVer =iary.getOpe_ver_fk();
			List<FbpbispamOA> oaryList = new ArrayList<FbpbispamOA>();

			String hql = "FROM Bis_pam where 1=1 ";
			if (StringUtils.isNotBlank(mdlId)) {
				if (mdlId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and mdl_id_fk='" + mdlId + "'";
				} else {// 模糊查询
					hql = hql + "and mdl_id_fk like '" + mdlId.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(toolId)) {
				if (toolId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and tool_id_fk='" + toolId + "'";
				} else {// 模糊查询
					hql = hql + "and tool_id_fk like '" + toolId.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(opeId)) {
				if (opeId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and ope_id_fk='" + opeId + "'";
				} else {// 模糊查询
					hql = hql + "and ope_id_fk like '" + opeId.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(pathId)) {
				if (pathId.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and path_id_fk='" + pathId + "'";
				} else {// 模糊查询
					hql = hql + "and path_id_fk like '" + pathId.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(pathVer)) {
				if (pathVer.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and path_ver_fk='" + pathVer + "'";
				} else {// 模糊查询
					hql = hql + "and path_ver_fk like '" + pathVer.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(opeVer)) {
				if (opeVer.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and ope_ver_fk='" + opeVer + "'";
				} else {// 模糊查询
					hql = hql + "and ope_ver_fk like '" + opeVer.replace("*", "%") + "'";
				}
			}
			if (StringUtils.isNotBlank(opeNo)) {
				if (opeNo.indexOf("*") == -1) {// 如果不包含*,精确查询
					hql = hql + "and ope_no_fk='" + opeNo + "'";
				} else {// 模糊查询
					hql = hql + "and ope_no_fk like '" + opeNo.replace("*", "%") + "'";
				}
			}
			hql = hql + " order by mdl_id_fk,path_id_fk,path_ver_fk,"
					+ "ope_no_fk,ope_id_fk,ope_ver_fk";
			//herbinate查询出一个集合
			List<Bis_pam> bispamList = bisPamDao.find(hql);
			for(Bis_pam bis_pam:bispamList){
				FbpbispamOA oary = new FbpbispamOA();
				oary.setTool_id_fk(bis_pam.getId().getTool_id_fk());
				oary.setMdl_id_fk(bis_pam.getId().getMdl_id_fk());
				oary.setPath_id_fk(bis_pam.getId().getPath_id_fk());
				oary.setPath_ver_fk(bis_pam.getId().getPath_ver_fk());
				oary.setOpe_no_fk(bis_pam.getId().getOpe_no_fk());
				oary.setOpe_id_fk(bis_pam.getId().getOpe_id_fk());
				oary.setOpe_ver_fk(bis_pam.getId().getOpe_ver_fk());
				//oary.setUnq_seq_id(bis_pam.getUnq_seq_id());
				oary.setRecipe_id(bis_pam.getRecipe_id());
				//光罩设定代码
				oary.setMask_set_id_fk(bis_pam.getMask_set_id_fk());
				oary.setActive_flg(bis_pam.getActive_flg());
				oary.setSkip_flg(bis_pam.getSkip_flg());
				oary.setMes_id(bis_pam.getMes_id());
				//箱子设定代码
				oary.setBox_set_code(bis_pam.getBox_set_code());
				oary.setSwh_cnt_add_flg(bis_pam.getSwh_cnt_add_flg());
				oary.setSwh_cnt_add_pos(bis_pam.getSwh_cnt_add_pos());
				oary.setSwh_max_cnt(bis_pam.getSwh_max_cnt());
				oaryList.add(oary);
			}
			/*outTrx.setTbl_cnt(oaryList.size());*/
			outTrx.setOary(oaryList);
			outTrx.setTbl_cnt(oaryList.size());
		}catch (Exception ex) {
			logUtils.error("[" + Util.stackTraceToString(ex) + "]");
			outTrx.setRtn_code(E_XPLSTDAT_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.toString());
			return;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
	}


	//删除操作
	public void deleteFnc(FbpbispamI inTrx,FbpbispamO outTrx){
		FbpbispamIA iary=inTrx.getIary().get(0);
		String mdlId=iary.getMdl_id_fk();
		//String toolId=iary.getTool_id_fk();
		String opeId=iary.getOpe_id_fk();
		String pathId =iary.getPath_id_fk();
		String pathVer = iary.getPath_ver_fk();
		String opeVer =iary.getOpe_ver_fk();
		//站点序号
		String opeNo =iary.getOpe_no_fk();
		Session session = bisPamDao.getCrSession();
		//先判断有没有存在该产品，不包含toolId,
		if(mdlId.equals(_SPACE) && opeId.equals(_SPACE) && pathId.equals(_SPACE)
				&& pathVer.equals(_SPACE) && opeVer.equals(_SPACE)){
			outTrx.setRtn_code(E_FPBISPAM_OTHER_ERR + "");
			outTrx.setRtn_mesg("删除时，参数全为空，请确认！");
			return;
		}
		String hql ="From Bis_pam where 1=1 ";
		if (StringUtils.isNotBlank(mdlId)) {
			hql = hql + "and mdl_id_fk='" + mdlId + "'";
		}
		if (StringUtils.isNotBlank(opeId) ) {
			hql = hql + "and ope_id_fk='" + opeId + "'";
		}
		if (StringUtils.isNotBlank(pathId) ) {
			hql= hql + "and path_id_fk='" + pathId + "'";
		}
		if (StringUtils.isNotBlank(pathVer)) {
			hql = hql + "and path_ver_fk='" + pathVer + "'";
		}
		if (StringUtils.isNotBlank(opeVer)) {
			hql = hql + "and ope_ver_fk='" + opeVer + "'";
		}
		if (StringUtils.isNotBlank(opeNo)) {
			hql = hql + "and ope_no_fk='" + opeNo + "'";
		}
		List<Bis_pam> list = bisPamDao.find(hql);
		if(!list.isEmpty()){
//			String hql2="delete from Bis_pam where mdl_id_fk="+"'"+mdlId+"'"
//					+" and ope_id_fk= "+"'"+opeId+"'"
//					+ " and path_id_fk= "+"'"+pathId+"'"
//					+ " and path_ver_fk= "+"'"+pathVer+"'"
//					+ " and ope_ver_fk= "+"'"+opeVer+"'"
//					+ " and ope_no_fk= "+"'"+opeNo+"'";//准备hql
//			Query query2=session.createQuery(hql2);
//			query2.executeUpdate();//更新
			/*for(int i = 0; i < list.size(); i++){
				bisPamDao.delete(list.get(i));
			}*/
			Timestamp evt_timestamp = Util.getCurrentTimestamp();
			for (Bis_pam bis_pam:list) {
				bis_pam.setEvt_timestamp(evt_timestamp);
				bis_pam.setEvt_usr(inTrx.getEvt_usr());
			}
			list.forEach(a -> bisPamDao.delete(a));
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg("success");
		}else {
			outTrx.setRtn_code(E_FPBISPAM_OTHER_ERR + "");
			outTrx.setRtn_mesg("not found record");
			return;
		}
	}

	//新增操作
	public void addFnc(FbpbispamI inTrx,FbpbispamO outTrx){
		List<FbpbispamIA> iaryList = inTrx.getIary();
		String evt_usr=inTrx.getEvt_usr();
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		for(int i = 0; i< iaryList.size(); i++){
			FbpbispamIA iary =  iaryList.get(i);
			String mdl_id=iary.getMdl_id_fk();
			String path_id=iary.getPath_id_fk();
			String path_ver=iary.getPath_ver_fk();
			String mask_set_id=iary.getMask_set_id_fk();
			String ope_id=iary.getOpe_id_fk();
			String ope_ver=iary.getOpe_ver_fk();
			String ope_no=iary.getOpe_no_fk();
			String tool_id=iary.getTool_id_fk();
			String recipe_id=iary.getRecipe_id();
			String active_flg=iary.getActive_flg();
			String skip_flg=iary.getSkip_flg();
			String mes_id=iary.getMes_id();
			String box_set_code=iary.getBox_set_code();
			String swh_cnt_add_flg=iary.getSwh_cnt_add_flg();
			Integer swh_max_cnt=iary.getSwh_max_cnt();
			String unq_seq_id=iary.getUnq_seq_id();
			Bis_pamId id= new Bis_pamId();
			id.setMdl_id_fk(mdl_id);
			id.setTool_id_fk(tool_id);
			id.setOpe_id_fk(ope_id);
			id.setPath_id_fk(path_id);
			id.setPath_ver_fk(path_ver);
			id.setOpe_ver_fk(ope_ver);
			id.setOpe_no_fk(ope_no);
			//通过几个id查询
			Bis_pam bis_pam=bisPamDao.get(id);
			if(bis_pam != null){//存在则更新
				bis_pam.setUnq_seq_id(unq_seq_id);
				bis_pam.setMask_set_id_fk(mask_set_id);
				bis_pam.setRecipe_id(recipe_id);
				bis_pam.setActive_flg(active_flg);
				bis_pam.setSkip_flg(skip_flg);
				bis_pam.setMes_id(mes_id);
				bis_pam.setBox_set_code(box_set_code);
				bis_pam.setSwh_cnt_add_flg(swh_cnt_add_flg);
				bis_pam.setSwh_max_cnt(swh_max_cnt);
				bis_pam.setEvt_usr(evt_usr);
				bis_pam.setEvt_timestamp(current_timestamp);
				bis_pam.setId(id);
				//执行保存操作
				bisPamDao.update(bis_pam);
			}else{//不存在则新增
				Bis_pam addbis_pam = new Bis_pam();
				addbis_pam.setUnq_seq_id(unq_seq_id);
				addbis_pam.setMask_set_id_fk(mask_set_id);
				addbis_pam.setRecipe_id(recipe_id);
				addbis_pam.setActive_flg(active_flg);
				addbis_pam.setSkip_flg(skip_flg);
				addbis_pam.setMes_id(mes_id);
				addbis_pam.setBox_set_code(box_set_code);
				addbis_pam.setSwh_cnt_add_flg(swh_cnt_add_flg);
				addbis_pam.setSwh_max_cnt(swh_max_cnt);
				addbis_pam.setEvt_usr(evt_usr);
				addbis_pam.setEvt_timestamp(current_timestamp);
				addbis_pam.setId(id);
				//执行保存操作
				bisPamDao.save(addbis_pam);
			}

		}
		//outTrx返回的对象
		FbpbispamOA oarY = new FbpbispamOA();
		FbpbispamIA iary = iaryList.get(0);
		oarY.setMdl_id_fk(iary.getMdl_id_fk());
		oarY.setPath_id_fk(iary.getPath_id_fk());
		oarY.setPath_ver_fk(iary.getPath_ver_fk());
		//站点序号
		oarY.setOpe_no_fk(iary.getOpe_no_fk());
		oarY.setOpe_id_fk(iary.getOpe_id_fk());
		oarY.setOpe_ver_fk(iary.getOpe_ver_fk());
		List<FbpbispamOA> oary =new ArrayList<FbpbispamOA>();
		oary.add(oarY);
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}

	//修改操作
	public void updateFnc(FbpbispamI inTrx,FbpbispamO outTrx){
		List<FbpbispamIA> iaryList = inTrx.getIary();
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		String evtUsr = inTrx.getEvt_usr();
		//删除数据库多余数据
		String mdlId = iaryList.get(0).getMdl_id_fk();
		String opeId = iaryList.get(0).getOpe_id_fk();
		String pathId = iaryList.get(0).getPath_id_fk();
		String pathVer = iaryList.get(0).getPath_ver_fk();
		String opeVer = iaryList.get(0).getOpe_ver_fk();
		String opeNo = iaryList.get(0).getOpe_no_fk();
		String hql = "From Bis_pam where mdl_id_fk="+"'"+mdlId+"'"
				+" and ope_id_fk= "+"'"+opeId+"'"
				+ " and path_id_fk= "+"'"+pathId+"'"
				+ " and path_ver_fk= "+"'"+pathVer+"'"
				+ " and ope_ver_fk= "+"'"+opeVer+"'"
				+ " and ope_no_fk= "+"'"+opeNo+"'";//准备hql
		List<Bis_pam> bis_pams = bisPamDao.find(hql);
		List<String> toolIdList = new ArrayList<String>();
		for(int i = 0; i < bis_pams.size(); i++){
			String toolIdFk = bis_pams.get(i).getId().getTool_id_fk();
			toolIdList.add(toolIdFk);
		}

		Iterator<String> stuIter = toolIdList.iterator();
		while (stuIter.hasNext()) {
			String toolId = stuIter.next();
			for (int i = 0; i < iaryList.size(); i++) {
				String toolIn = iaryList.get(i).getTool_id_fk();
				if(toolIn.equals(toolId)){
					stuIter.remove();
				}
			}
		}

		if(toolIdList.size() > 0){
			for(int i = 0; i < toolIdList.size(); i++){
				Bis_pamId bis_pamId = new Bis_pamId();
				bis_pamId.setMdl_id_fk(mdlId);
				bis_pamId.setTool_id_fk(toolIdList.get(i));
				bis_pamId.setOpe_id_fk(opeId);
				bis_pamId.setPath_id_fk(pathId);
				bis_pamId.setPath_ver_fk(pathVer);
				bis_pamId.setOpe_ver_fk(opeVer);
				bis_pamId.setOpe_no_fk(opeNo);
				Bis_pam bis_pam = bisPamDao.get(bis_pamId);
				bis_pam.setEvt_timestamp(Util.getCurrentTimestamp());
				bis_pam.setEvt_usr(inTrx.getEvt_usr());
				bisPamDao.delete(bis_pam);
			}
		}

		for(int i = 0; i < iaryList.size(); i++){
			FbpbispamIA iary = iaryList.get(i);
			String mdlIdFk=iary.getMdl_id_fk();
			String toolIdF=iary.getTool_id_fk();
			String opeIdFk=iary.getOpe_id_fk();
			String pathIdFk =iary.getPath_id_fk();
			String pathVerFk = iary.getPath_ver_fk();
			String opeVerFk =iary.getOpe_ver_fk();
			//站点序号
			String opeNoFk =iary.getOpe_no_fk();
			Bis_pamId id= new Bis_pamId();
			id.setMdl_id_fk(mdlIdFk);
			id.setTool_id_fk(toolIdF);
			id.setOpe_id_fk(opeIdFk);
			id.setPath_id_fk(pathIdFk);
			id.setPath_ver_fk(pathVerFk);
			id.setOpe_ver_fk(opeVerFk);
			id.setOpe_no_fk(opeNoFk);
			Bis_pam bis_pam=bisPamDao.get(id);
			if (bis_pam == null) { //新增
				Bis_pam newBisPam = new Bis_pam();
				String maskSetId=iary.getMask_set_id_fk();
				String activeFlg =iary.getActive_flg();
				String recipeId=iary.getRecipe_id();
				String skipFlg =iary.getSkip_flg();
				String mesId = iary.getMes_id();
				String boxSetCode =iary.getBox_set_code();
				String swhCntAddFlg=iary.getSwh_cnt_add_flg();
				String swhCntAddPos= iary.getSwh_cnt_add_pos();
				Integer swhMaxCnt= iary.getSwh_max_cnt();
				if(maskSetId!=null&& maskSetId.length() != 0){
					newBisPam.setMask_set_id_fk(maskSetId);
				}
				if(activeFlg!=null&& activeFlg.length() != 0){
					newBisPam.setActive_flg(activeFlg);
				}
				//配方代码
				if (recipeId!=null&& recipeId.length() != 0) {
					newBisPam.setRecipe_id(recipeId);
				}
				if(skipFlg!=null&& skipFlg.length() != 0){
					newBisPam.setSkip_flg(skipFlg);
				}
				if(mesId!=null&& mesId.length() != 0){
					newBisPam.setMes_id(mesId);
				}
				if(boxSetCode!=null&& boxSetCode.length() != 0){
					newBisPam.setBox_set_code(boxSetCode);
				}
				if(swhCntAddFlg!=null&& swhCntAddFlg.length() != 0){
					newBisPam.setSwh_cnt_add_flg(swhCntAddFlg);
				}
				if(swhCntAddPos!=null&& swhCntAddPos.length() != 0){
					newBisPam.setSwh_cnt_add_pos(swhCntAddPos);
				}
				if(swhMaxCnt!=null){
					//最大重工次数必须为数字
					newBisPam.setSwh_max_cnt(swhMaxCnt);
				}
				newBisPam.setEvt_usr(evtUsr);
				newBisPam.setEvt_timestamp(current_timestamp);
				newBisPam.setId(id);
				//执行修改操作
				bisPamDao.save(newBisPam);
			}else {
				String maskSetId=iary.getMask_set_id_fk();
				String activeFlg =iary.getActive_flg();
				String recipeId=iary.getRecipe_id();
				String skipFlg =iary.getSkip_flg();
				String mesId = iary.getMes_id();
				String boxSetCode =iary.getBox_set_code();
				String swhCntAddFlg=iary.getSwh_cnt_add_flg();
				String swhCntAddPos= iary.getSwh_cnt_add_pos();
				Integer swhMaxCnt= iary.getSwh_max_cnt();
				if(maskSetId!=null&& maskSetId.length() != 0){
					bis_pam.setMask_set_id_fk(maskSetId);
				}
				if(activeFlg!=null&& activeFlg.length() != 0){
					bis_pam.setActive_flg(activeFlg);
				}
				//配方代码
				if (recipeId!=null&& recipeId.length() != 0) {
					bis_pam.setRecipe_id(recipeId);
				}
				if(skipFlg!=null&& skipFlg.length() != 0){
					bis_pam.setSkip_flg(skipFlg);
				}
				if(mesId!=null&& mesId.length() != 0){
					bis_pam.setMes_id(mesId);
				}
				if(boxSetCode!=null&& boxSetCode.length() != 0){
					bis_pam.setBox_set_code(boxSetCode);
				}
				if(swhCntAddFlg!=null&& swhCntAddFlg.length() != 0){
					bis_pam.setSwh_cnt_add_flg(swhCntAddFlg);
				}
				if(swhCntAddPos!=null&& swhCntAddPos.length() != 0){
					bis_pam.setSwh_cnt_add_pos(swhCntAddPos);
				}
				if(swhMaxCnt!=null){
					//最大重工次数必须为数字
					bis_pam.setSwh_max_cnt(swhMaxCnt);
				}
				bis_pam.setEvt_usr(evtUsr);
				bis_pam.setEvt_timestamp(current_timestamp);
				bis_pam.setId(id);
				//执行修改操作
				bisPamDao.update(bis_pam);
			}
		}

		//outTrx返回的对象
		FbpbispamIA iary = iaryList.get(0);
		FbpbispamOA oarY = new FbpbispamOA();
		oarY.setMdl_id_fk(iary.getMdl_id_fk());
		oarY.setPath_id_fk(iary.getPath_id_fk());
		oarY.setPath_ver_fk(iary.getPath_ver_fk());
		//站点序号
		oarY.setOpe_no_fk(iary.getOpe_no_fk());
		oarY.setOpe_id_fk(iary.getOpe_id_fk());
		oarY.setOpe_ver_fk(iary.getOpe_ver_fk());
		List<FbpbispamOA> oary =new ArrayList<FbpbispamOA>();
		oary.add(oarY);
		outTrx.setOary(oary);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}

	//错误信息
	public void errActionFnc(FbpbispamO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
}
